package com.qf.mybatisplus.rabbitMQ;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

//@Component
public class TopicConsumer {


//    @Autowired
//    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue,
                            exchange = @Exchange(value = "boot-topic-exchange", type = "topic"),
                            key = {"*.red.*", "black.*.#"}
                    )
            }
    )
    public void getMessage2(String msg, Channel channel, Message message) throws Exception {
        TimeUnit.SECONDS.sleep(1);
        System.err.println("消费者1收到: "+msg);
        // 手动  ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }


    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue,
                            exchange = @Exchange(value = "boot-topic-exchange", type = "topic"),
                            key = {"black.*.#"}
                    )
            }
    )
    public void getMessage3(String msg, Channel channel, Message message) throws Exception {
        TimeUnit.SECONDS.sleep(1);
        System.err.println("消费者2收到: "+msg);
        // 手动  ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    /**
    * @Description 避免消息重复消费
    * @Param [msg, channel, message]
    * @Author 12136
    * @Date 14:34 2021/11/24
    * @return void
    **/
    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue,
                            exchange = @Exchange(value = "boot-topic-exchange-repeat",type = "topic"),
                            key = {"black.*.#"}
                    )
            }
    )
    public void getMessage4(String msg, Channel channel, Message message) throws IOException {
        //0. 获取MessageId
        String messageId = message.getMessageProperties().getHeader("spring_returned_message_correlation");
        //1. 设置key到Redis
        if(redisTemplate.opsForValue().setIfAbsent(messageId,"0",10, TimeUnit.SECONDS)) {
            //2. 消费消息
            System.err.println("消费者接收到消息：" + msg);

            //3. 设置key的value为1
            redisTemplate.opsForValue().set(messageId,"1",10,TimeUnit.SECONDS);
            //4.  手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }else {
            //5. 获取Redis中的value即可 如果是1，手动ack
            if("1".equalsIgnoreCase((String) redisTemplate.opsForValue().get(messageId))){
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }
        }

    }
}